﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TEL.ClientClasses;
using TEL.ClientClasses.Private;

namespace TEL.TelPhoneF.Controls
{
    public partial class ButtonLine : Button
    {
        public ButtonLine()
        {
            InitializeComponent();
            this.Paint += new PaintEventHandler(ButtonLine_Paint);
        }

        
        public ButtonLine(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            this.Paint += new PaintEventHandler(ButtonLine_Paint);
        }

        void ButtonLine_Paint(object sender, PaintEventArgs e)
        {
            //if (State == CallState.Active 
            //    || State == CallState.Hold 
            //    || State == CallState.WaitingToAccept 
            //    || State == CallState.WaitingForStart 
            //    || State == CallState.Calling 
            //    || State == CallState.Ringing 
            //    || State == CallState.DialTone)
            if(State != CallState.Entity)
            {
                Paths paths = new Paths();
                GraphicsPath gp = paths.FairingPath(new Point(1, 1), this.Size - new Size(2, 2));
                e.Graphics.DrawPath(new Pen(Form1.BlueSkinColor), gp);
            }
        }


        public string LineCall { get; set; }
        public bool IsOutCall { get; set; }
        public string NumberPhone
        {
            get
            {
                if (string.IsNullOrEmpty(LineCall)) return string.Empty;
                return ClientClasses.SipVizard.GetNumberFromSip(LineCall);
            }
        }
        public int LineNumber { get; set; }

        private CallState _state = CallState.Entity;
        public CallState State
        {
            get { return _state; }
            set
            {
                _state = value;
                switch (value)
                {
                   case CallState.Active:
                        this.Image = Properties.Resources.box_green_8;
                        break;
                    case CallState.Hold:
                        this.Image = Properties.Resources.box_red_8;
                        break;
                    case CallState.Entity:
                        this.Image = null;
                        break;
                    case CallState.Disposed :
                        this.Image = Properties.Resources.box_grey_8;
                        break;
                    default:
                        this.Image = Properties.Resources.box_yellow_8;
                        break;
                }
            }
        }

        private SipLine m_LinePhone = null;
        public SipLine LinePhone
        {
            get { return m_LinePhone; }
            set
            {
                m_LinePhone = value;
                m_LinePhone.LineStateChanged += new EventHandler<CallEventArgs>(m_LinePhone_LineEvent);
            }
        }

        void m_LinePhone_LineEvent(object sender, CallEventArgs e)
        {
            try
            {
                SipLine sipLine = sender as SipLine;
                if (sipLine != null) State = sipLine.State;
                else State = CallState.Entity;
            }
            catch (Exception)
            {
            }
        }

    }
}